// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

/*引入element-ui*/
import Element from 'element-ui'
import "element-ui/lib/theme-chalk/index.css"
Vue.use(Element)
/*引入vue-blu*/
import VueBlu from 'vue-blu'
import 'vue-blu/dist/css/vue-blu.min.css'
Vue.use(VueBlu)

/*引入axios*/
import axios from 'axios'
//配置axios的全局基本路径
axios.defaults.baseURL='http://127.0.0.1:8081'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios




Vue.config.productionTip = false

/* eslint-disable no-new */

new Vue({
  router,
  render: h => h(App)
}).$mount("#app")
//=======================================
//1 使用axios前置拦截器，让所有的请求都携带token
axios.interceptors.request.use(config=>{
  //携带token
  let token =  localStorage.getItem("token");
  if(token){
    config.headers['token']=token;//在所有的请求头中加入token
  }
  return config;
},error => {
  Promise.reject(error);
})
//2 使用axios后置拦截器，处理没有登录请求
/*
axios.interceptors.response.use(result=>{
  let data = result.data;
  //如果返回值为false且携带的信息是nologin则清空浏览器中过期的数据以及跳转到登陆界面
  if(!data.success && data.message==="nologin"){
    localStorage.removeItem("token");
    localStorage.removeItem("logininfo");
    router.push("/login");
  }
  return result;
},error => {
  Promise.reject(error);
})
*/


/*
/!*3静态资源放行*!/
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path === '/login' || to.path ==="/shop/settlement" || to.path === "/register") {
    next();
  }else {
    let user = JSON.parse(localStorage.getItem('logininfo'));
    if (!user ) {
      next({ path: '/login' })
    }
    next()
  }
})*/
